package com.isman.jackson.hibernate4;

import com.google.appengine.repackaged.org.codehaus.jackson.map.BeanDescription;
import com.google.appengine.repackaged.org.codehaus.jackson.map.BeanProperty;
import com.google.appengine.repackaged.org.codehaus.jackson.map.JsonSerializer;
import com.google.appengine.repackaged.org.codehaus.jackson.map.SerializationConfig;
import com.google.appengine.repackaged.org.codehaus.jackson.map.Serializers.Base;
import com.google.appengine.repackaged.org.codehaus.jackson.type.JavaType;

import org.hibernate.engine.spi.Mapping;
import org.hibernate.proxy.HibernateProxy;

import com.isman.jackson.hibernate4.Hibernate4Module.Feature;

public class HibernateSerializers extends Base {
	protected final boolean _forceLoading;
	protected final boolean _serializeIdentifiers;
	protected final Mapping _mapping;

	public HibernateSerializers(int features) {
		this(null, features);
	}

	public HibernateSerializers(Mapping mapping, int features) {
		_forceLoading = Feature.FORCE_LAZY_LOADING.enabledIn(features);
		_serializeIdentifiers = Feature.SERIALIZE_IDENTIFIER_FOR_LAZY_NOT_LOADED_OBJECTS.enabledIn(features);
		_mapping = mapping;
	}

	@Override
	public JsonSerializer<?> findSerializer(SerializationConfig config,JavaType type, BeanDescription beanDesc, BeanProperty property) {
		Class<?> raw = type.getRawClass();
		if (HibernateProxy.class.isAssignableFrom(raw)) {
			return new HibernateProxySerializer(_forceLoading,
					_serializeIdentifiers, _mapping);
		}
		return null;
	}
}
